package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TravelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 待审核游记分页/高级查询
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("travelAudit/list")
    public String audit(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        //设置默认待发布状态
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_WAIT);
        }
        //默认为公开状态
        qo.setIsPublic(true);
        //创建时间正序排列
        qo.setOrderBy("t.createTime asc");
        model.addAttribute("result", travelService.queryForList(qo));
        return "travel/audit";
    }

    /**
     * 待审核游记查看游记内容
     *
     * @param id
     * @return
     */
    @RequestMapping("travel/getContentById")
    @ResponseBody
    public TravelContent getContentById(Long id) {
        return travelService.getContentById(id);
    }

    /**
     * 待审核游记发布或拒绝
     *
     * @param travel
     * @return
     */
    @RequestMapping("travel/changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travelService.changeState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 已发布游记页面
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("travelRelease/list")
    public String release(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        //状态为已发布游记
        qo.setState(Travel.STATE_RELEASE);
        //默认为公开状态
        qo.setIsPublic(true);
        //按照发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result", travelService.queryForList(qo));
        return "travel/release";
    }

     //跟据地区的id查询游记
    @RequestMapping("travel/selectRelease")
    @ResponseBody
    public List<Travel> selectRelease(Long id) {
        return travelService.selectRelease(id);
    }
}
